import { Form, Input, DatePicker, Space, Button, message, Upload } from "antd";
import React, { useState } from "react";
import { UploadOutlined } from '@ant-design/icons';

const SendCoupons = (props) => {
    const [fileList, setFileList] = useState([]);
    const [uploading, setUploading] = useState(false);
    const { couponId, indcutsValue, handleCancel } = props
    // console.log(props);
    const handleUpload = async () => {
        const formData = new FormData();
        fileList.forEach((file) => {
            formData.append('file', file);
        });
        formData.append('couponId', couponId)
        setUploading(true); // You can use any AJAX library you like
        // let res = await apiUploadExcel({...formData})
        // if (res.code === 0 && res.data) {
        //     React.$message({
        //         type: 'info',
        //         content: '结束成功'
        //     })
        //     handleCancel()
        // }

        fetch(React.$BaseUrl + '/trade/couponInfo/uploadExcel', {
            method: "post",
            headers: {
                RSnetToken: sessionStorage.getItem('RSnetToken')
            },
            body: formData,
        })
            .then((res) => {
                res.json().then(res => {
                    if (res.code === 0) {
                        if (res.data === 0){
                            message.success('全部发放成功')
                        }else if (res.data === 1){
                            message.success('部分发放成功')
                        }else {
                            message.success('全部发放失败')
                        }
                        setFileList([]);
                        // let len = res.data.length
                        // message.success('全部成功发放' + len + '张优惠券')
                        handleCancel()
                    } else if (res.data === null) {
                        setFileList([]);
                        message.error("文件内容异常，请检查文件重新上传");
                    } else {
                        message.error(res.codeMessage)
                    }
                })
            })
            .catch((res) => {
                console.log(res);
                message.error('上传失败.');
            })
            .finally(() => {
                setUploading(false);
            });
    };

    const filedata = {
        accept: ".xls,.xlsx,application/vnd.ms-excel",
        maxCount: 1,
        onRemove: (file) => {
            const index = fileList.indexOf(file);
            const newFileList = fileList.slice();
            newFileList.splice(index, 1);
            setFileList(newFileList);
        },
        beforeUpload: (file) => {
            setFileList([...fileList, file]);
            return false;
        },
        fileList,
    };
    return (
        <>
            <div>
                <span style={{ lineHeight: "50px", marginRight: "10px" }}>优惠券名称:</span>
                {indcutsValue.couponTitle}
            </div>
            <div>
                <span style={{ lineHeight: "50px", marginRight: "10px" }}>上传Excel:</span>
                <Upload {...filedata}>
                    <Button icon={<UploadOutlined />}>选择文件</Button>
                </Upload>
            </div>

            <Button
                type="primary"
                onClick={handleUpload}
                disabled={fileList.length === 0}
                loading={uploading}
                style={{
                    marginTop: 16,
                }}
            >
                {uploading ? 'Uploading' : '提交'}
            </Button>
        </>
    );
};
export default SendCoupons;
